<?php
class Line_order_refund_model extends CI_Model
{
    private $table   = 'line_order_refund';
    private $table_1 = 'sales_order';
    private $table_2 = 'line_order_product';
    
    public function insertLineOrderRefund($postData)
    {
        $data = array(
            'order_id'       => $postData['order_id'],
            'product_id'     => $postData['product_id'],
            'refund_status'  => 1, //1：申请退款；2：同意退款；3：否决退款
            'refund_name'    => $postData['refund_name'],
            'refund_tel'     => $postData['refund_tel'],
            'refund_content' => $postData['refund_content'],
            'reject_content' => '',
            'flag'           => 1 //未退款
        );
        $this->db->insert($this->table, $data);
        return $this->db->insert_id();
    }
    
    /**
     * 验证用户是否投诉
     * @param unknown $order_id
     */
    public function validateResellerRefund($order_id)
    {
        $this->db->where('order_id', $order_id);
        $result = $this->db->get($this->table);
        if ($result->num_rows() > 0) {
            return $result->row(0);
        }
        return false;
    }
    
    /**
     * 获取订单投诉详情
     * @param unknown $order_id
     */
    public function getRefundDetailByOrderId($order_id)
    {
        $this->db->where('order_id', $order_id);
        return $this->db->get($this->table);
    }
    
    /**
     * 修改否决退款状态
     * @param unknown $order_id
     * @param unknown $complain_status
     */
    public function updateLineOrderRefund($postData, $refund_status)
    {
        $data = array(
            'reject_content' => $postData['reject_content'],
            'refund_status'  => $refund_status,
        );
        $this->db->where('refund_id', $postData['refund_id']);
        return $this->db->update($this->table, $data);
    }
    
    /**
     * 修改同意退款状态
     * @param unknown $order_id
     * @param unknown $complain_status
     */
    public function updateRefundStatus($order_id, $refund_status)
    {
        $data = array(
            'refund_status'  => $refund_status,
        );
        $this->db->where('order_id', $order_id);
        return $this->db->update($this->table, $data);
    }

    /**
     * 线路订单列表 总页数
     */
    public function total($params)
    {
        $this->db->select('refund_id');
        $this->db->from($this->table);
        $this->db->join($this->table_1, 'sales_order.order_id=line_order_refund.order_id', 'INNER');
        //$this->db->join($this->table_2, 'line_order_product.order_id = sales_order.order_id', 'INNER');
        $this->db->where('sales_order.uid', $params['uid']);

        return $this->db->count_all_results();
    }

    /**
     * 线路订单列表
     */
    public function refundList($params, $count, $start)
    {
        $this->db->select('sales_order.order_id, sales_order.order_sn, sales_order.order_pay_total,order_state,order_status, 
            line_order_product.line_name, line_order_product.start_date, line_order_product.adult_num, line_order_product.children_num, line_order_product.travel_days,
            line_order_refund.created_at, line_order_refund.refund_id, line_order_refund.flag');
        $this->db->from($this->table);
        $this->db->join($this->table_1, 'sales_order.order_id=line_order_refund.order_id', 'INNER');
        $this->db->join($this->table_2, 'line_order_product.order_id = sales_order.order_id', 'INNER');
        $this->db->where('sales_order.uid', $params['uid']);
        $this->db->group_by('line_order_product.id');
        $this->db->order_by('line_order_refund.refund_id');
        $this->db->limit($start, $count);
        return $this->db->get();
    }

    /**
     * 查找订单详情
     */
    public function findByOrderInfo($refundId, $uid)
    {
        $this->db->select('sales_order.order_id, sales_order.order_sn, sales_order.order_pay_total,order_state,order_status, 
            line_order_product.line_name, line_order_product.start_date, line_order_product.adult_num, line_order_product.children_num, line_order_product.travel_days,
            line_order_refund.created_at, line_order_refund.refund_id, line_order_refund.flag');
        $this->db->from($this->table);
        $this->db->join($this->table_1, 'sales_order.order_id=line_order_refund.order_id', 'INNER');
        $this->db->join($this->table_2, 'line_order_product.order_id = sales_order.order_id', 'INNER');
        $this->db->where('line_order_refund.refund_id', $refundId);
        $this->db->where('sales_order.uid', $uid);
        $this->db->limit(1);
        return $this->db->get();
    }
    
}
